package com.jushu.storbox;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.view.viewImpl.SafeLockView;
import com.jushu.storbox.view.viewImpl.SafeLockView.OnCompleteListener;
public class SetLoackActivity extends BaseActivity implements OnClickListener
{
	private SafeLockView lpwv;
	private String password;
	private String oldPassword;
	private String LOG_TAG = "SetLoackActivity";
	private Toast toast;
	private int state = 1;
	private final static int FIRST_INPUT_PASSWORD=1;
	private final static int SECOND_INPUT_PASSWORD=2;
	private Button buttonSave,tvReset;
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.setloack_activity);
		ActivityTaskManager.getInstance().putActivity(LOG_TAG,SetLoackActivity.this);
		lpwv = (SafeLockView) this.findViewById(R.id.mLocusPassWordView);
		lpwv.setOnCompleteListener(new PasswordListener());
		buttonSave = (Button) this.findViewById(R.id.lock_password_ok_bt);
		buttonSave.setOnClickListener(this);
		tvReset = (Button) this.findViewById(R.id.lock_password_cancel_bt);
		tvReset.setOnClickListener(this);
	}
	private class PasswordListener implements OnCompleteListener
	{
		@Override
		public void onComplete(String password)
		{
			SetLoackActivity.this.password = password;
			
		}
	}
	@Override
	public void onClick(View v) 
	{
		switch (v.getId())
		{
		case R.id.lock_password_ok_bt:
			if (password != null&&!password.equals("") ) 
			{
				if(password.length()>=4 )
				{
					lpwv.resetPassWord(password);

					if(state==FIRST_INPUT_PASSWORD){
						oldPassword=password;
						password=null;
						state=SECOND_INPUT_PASSWORD;
						lpwv.clearPassword();
						showToast("success,please input again");
					}
					else if(state==SECOND_INPUT_PASSWORD&&oldPassword.equals(password)){
					showToast("set success,please remember password");
					lpwv.clearPassword();
					SharedPreferences settings = SetLoackActivity.this.getSharedPreferences(
					ClientConfig.settings, SetLoackActivity.this.getApplicationContext().MODE_APPEND);
					Editor editor = settings.edit();
					editor.putBoolean(ClientConfig.AUTO_SAFETY_LOCK, true);
					editor.commit();
					finish();
					}else{
						showToast("input error,please input again!");
						lpwv.markError(2000);
//						lpwv.clearPassword();
					}
				}
				else
				{
					showToast("password can't less than 4 point");
					password=null;
					lpwv.clearPassword();
				}
			} 
			else
			{
				lpwv.clearPassword();
				showToast("password can't empty,please input");
			}
			break;
		case R.id.lock_password_cancel_bt:
			lpwv.clearPassword();
			break;
		}
	}
	private void showToast(CharSequence message)
	{
		if (null == toast)
		{
			toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		} 
		else 
		{
			toast.setText(message);
		}

		toast.show();
	}
	@Override
	protected void onStart()
	{
		super.onStart();
	}
	@Override
	protected void onStop()
	{
		super.onStop();
	}
}

